//
//  NewRegistViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/6/23.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "NewRegistViewController.h"
#import "AgreementView.h"
#import "WelcomViewController.h"
@interface NewRegistViewController ()<agreementDelegate>

@property(nonatomic ,strong) UITextField *oldPassWord;
@property(nonatomic ,strong) UITextField *passWord;
@property(nonatomic ,strong) UITextField *passWord1;


@end

@implementation NewRegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=MyString(@"注册");
    
    // Do any additional setup after loading the view.
}
-(void)createView{
    CGFloat width=MainW-20;
    NSArray *ary=[NSArray arrayWithObjects:MyString(@"邀请码"),MyString(@"密码"),MyString(@"密码"), nil];
    NSArray *arry=[NSArray arrayWithObjects:MyString(@"请输入邀请码(可不填)"),MyString(@"请设置您的密码"),MyString(@"请确认您的密码"), nil];

    for (int i=0; i<ary.count; i++) {
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(10, 30+i*80, width, 49)];
        view.backgroundColor=[UIColor whiteColor];
        ViewRadius(view, 2);
        [self.view addSubview:view];
        UILabel *tileLable=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 30)];
        tileLable.text=ary[i];
        tileLable.font=[UIFont systemFontOfSize:14];
        tileLable.textColor=TextColor3;
        
        [view addSubview:tileLable];
        
        UITextField*textField=[[UITextField alloc]initWithFrame:CGRectMake(tileLable.bsg_right, 10, width-70, 30)];
        textField.font=[UIFont systemFontOfSize:14];
        textField.placeholder=arry[i];
        [textField setValue:[UIFont systemFontOfSize:14]forKeyPath:@"_placeholderLabel.font"];
        [view addSubview:textField];
        if (i==0) {
            self.oldPassWord=textField;
        }else if (i==1){
            self.passWord=textField;
        }else if(i==2){
            self.passWord1=textField;
        }
    }
    if (self.inviteCode.length!=0) {
        self.oldPassWord.text=self.inviteCode;
    }
    UILabel*beizhu=[[UILabel alloc]init];
    beizhu.text=MyString(@"邀请备注");
    beizhu.textColor=TextColor9;
    beizhu.numberOfLines=0;
    beizhu.font=[UIFont systemFontOfSize:12];
    CGFloat het=[ZJUITools getHeightByWidth:MainW-20 title:beizhu.text font:beizhu.font];
    beizhu.frame=CGRectMake(10, 250, width, het+10);
    [self.view addSubview:beizhu];
    beizhu.attributedText=[self getAttributedStringWithString:beizhu.text lineSpace:5];
    UIButton *MySureBtn=[[UIButton alloc]initWithFrame:CGRectMake(10, beizhu.bsg_bottom+20, MainW-20, 50)];
        [MySureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [MySureBtn setTitle:MyString(@"确认") forState:UIControlStateNormal];
        [MySureBtn addTarget:self action:@selector(SureClick) forControlEvents:UIControlEventTouchUpInside];
        [MySureBtn setBackgroundColor:Red_Color];
        [self.view addSubview:MySureBtn];
        ViewRadius(MySureBtn, 5);
    AgreementView *agree=[[AgreementView alloc]initWithFrame:CGRectMake(0, MainH-64-80, MainW, 20)];
    agree.delegate=self;
    [self.view addSubview:agree];
    
}
-(void)AgreementDelegateClick{
    WelcomViewController*vc=[WelcomViewController new];
    vc.reLastURL=[NSString stringWithFormat:@"%@/agreement",baseURL];
    vc.title=MyString(@"注册协议");
    [self.navigationController pushViewController:vc animated:YES];
    //NSLog(@"他同意了");
}
-(NSAttributedString *)getAttributedStringWithString:(NSString *)string lineSpace:(CGFloat)lineSpace {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpace; // 调整行间距
    NSRange range = NSMakeRange(0, [string length]);
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
    return attributedString;
}
-(void)SureClick{
    //NSLog(@"  %@  %@    %@",self.oldPassWord.text,self.passWord.text,self.passWord1.text);
//    if (self.statue==1) {
        if (self.passWord1.text.length==0||self.passWord.text.length==0) {
            [self showToast:MyString(@"密码不能为空")];
        }else{
            if (self.passWord.text.length<6||self.passWord1.text.length<6) {
                [self showToast:MyString(@"密码长度不对")];
            }else{
                if ([self.passWord.text isEqualToString:self.passWord1.text]) {
                    if ([self deptNumInputShouldNumber:self.passWord.text]) {
                        [self showToast:MyString(@"密码不能纯数字")];
                    }else{
                        [self registAccountClick];
                    }
                }else{
                    [self showToast:MyString(@"您两次密码输入不一致,请重新输入")];
                }
            }
        }
    
}
-(void)registAccountClick{
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    [parametersDic setObject:self.oldPassWord.text forKey:@"invitationCode"];
    [parametersDic setObject:self.phone forKey:@"phone"];
    [parametersDic setObject:self.passWord1.text forKey:@"password"];
    [parametersDic setObject:self.smsCode forKey:@"smsCode"];
    [self doHttpRequestWithApi:API_REGIST method:POST dic:parametersDic successBlock:^(NSDictionary *dic) {
        //NSLog(@"注册");
        if (self.type==1) {
            [self QQLoginClick];
        }else if (self.type==2){
            [self wechatLoginClick];
        }else{
            [MBProgressHUD showSuccess:@"注册成功"];
            
            //储存密码
            [[NSUserDefaults standardUserDefaults]setObject:self.phone forKey:@"passwordabc"];
            //储存账户
            [[NSUserDefaults standardUserDefaults ]setObject:self.passWord1.text forKey:@"userNameabc"];
            //必须
            [[NSUserDefaults standardUserDefaults]synchronize];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                UIViewController *viewCtl = self.navigationController.viewControllers[0];
                [self.navigationController popToViewController:viewCtl animated:YES];
            });
            
        }
        
    }];
}
-(void)wechatLoginClick{
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    dic[@"smsCode"]=self.smsCode;
    dic[@"phone"]=self.phone;
    dic[@"code"]=[[NSUserDefaults standardUserDefaults]objectForKey:@"WXCode"];
    [self doHttpRequestWithApi:API_WechatLOGINbinding method:POST dic:dic successBlock:^(NSDictionary *dic) {
        if ([dic[@"status"] intValue]==143) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                UIViewController *viewCtl = self.navigationController.viewControllers[0];
                [self.navigationController popToViewController:viewCtl animated:YES];
            });
        }else if ([dic[@"status"] intValue]==142){
            [ZJUserDefaultsTools saveUserInfoLocal:dic];
            [self dismissViewControllerAnimated:YES completion:nil];
        }else if ([dic[@"status"] intValue]==141){
            
        }else{
            [ZJUserDefaultsTools saveUserInfoLocal:dic];
            [[EMClient sharedClient] loginWithUsername:[ZJUserDefaultsTools getCurrentIMName]
                                              password:[ZJUserDefaultsTools getCurrentIMPASSWORD]
                                            completion:^(NSString *aUsername, EMError *aError) {
                                                if (!aError) {
                                                    //NSLog(@"登录成功");
                                                    EMPushOptions *emoptions = [[EMClient sharedClient] pushOptions];
                                                    //设置有消息过来时的显示方式:1.显示收到一条消息 2.显示具体消息内容.
                                                    //自己可以测试下
                                                    emoptions.displayStyle = EMPushDisplayStyleSimpleBanner;
                                                    [[EMClient sharedClient] updatePushOptionsToServer];
                                                } else {
                                                    //NSLog(@"登录失败");
                                                }
                                            }];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        
        
    }];
    
}
-(void)QQLoginClick{
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    dic[@"smsCode"]=self.smsCode;
    dic[@"phone"]=self.phone;
    dic[@"openKey"]=[[NSUserDefaults standardUserDefaults]objectForKey:@"QQopenKey"];
    dic[@"openId"]=[[NSUserDefaults standardUserDefaults]objectForKey:@"QQopenid"];;
    dic[@"pf"]=@"";
    [self doHttpRequestWithApi:API_QQLOGINbinding method:GET dic:dic successBlock:^(NSDictionary *dic) {
        if ([dic[@"status"] intValue]==143) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                UIViewController *viewCtl = self.navigationController.viewControllers[0];
                [self.navigationController popToViewController:viewCtl animated:YES];
            });
        }else if ([dic[@"status"] intValue]==142){
            
        }else if ([dic[@"status"] intValue]==141){
            
        }else{
            [ZJUserDefaultsTools saveUserInfoLocal:dic];
            [[EMClient sharedClient] loginWithUsername:[ZJUserDefaultsTools getCurrentIMName]
                                              password:[ZJUserDefaultsTools getCurrentIMPASSWORD]
                                            completion:^(NSString *aUsername, EMError *aError) {
                                                if (!aError) {
                                                    //NSLog(@"登录成功");
                                                    EMPushOptions *emoptions = [[EMClient sharedClient] pushOptions];
                                                    //设置有消息过来时的显示方式:1.显示收到一条消息 2.显示具体消息内
                                                    [[EMClient sharedClient].options setIsAutoLogin:YES];
                                                    //自己可以测试下
                                                    emoptions.displayStyle = EMPushDisplayStyleSimpleBanner;
                                                    [[EMClient sharedClient] updatePushOptionsToServer];
                                                } else {
                                                    //NSLog(@"登录失败");
                                                }
                                            }];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        
        
    }];
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.passWord resignFirstResponder];
    [self.oldPassWord resignFirstResponder];
    [self.passWord1 resignFirstResponder];
}
#pragma mark -----修改登录密码-----
-(void)exitLoginPsWord{
    
}

@end
